package LeetCode;

import static LeetCode.CreatAndPrintList.creatList;
import static LeetCode.CreatAndPrintList.printList;

public class Solution_0204 {
    public static void main(String[] args) {
        ListNode head=creatList();
//        print(head);
        printList(reverseList(head));
    }

//反转链表
    private static ListNode reverseList(ListNode head) {
//         方法一：逐个将旧链表的节点插入到新链表
//         ListNode new_head = null;  //新链表的尾节点
//         while(head!=null){
//             ListNode tmp = head;  //记录节点
//             head = head.next;  //遍历后移
//             tmp.next = new_head;  //将节点放入新链表表头
//             new_head = tmp;  //更新新链表的表头
//         }
//         return new_head;

//方法二：遍历，一次将当前结点指向前一个结点
        if(head==null){
            return head;
        }
        if(head.next==null){
            return head;
        }

        ListNode prevNode=null;
        ListNode curNode=head;
        while (curNode!=null){
            ListNode nextNode=curNode.next;
            curNode.next=prevNode;
            prevNode=curNode;
            curNode=nextNode;

        }
        return prevNode;
    }
}
